package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseFragmentActivity;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.TabViewPagerAdapter;
import com.hemaapp.yjnh.fragment.LaokeListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**助农
 * 唠嗑，帖子论坛
 *
 * @author GuiyouZhang
 *         Date：2016年5月4日 上午10:36:47
 *         Email:guiyouzhang@163.com
 */
public class LaokeActivity extends BaseFragmentActivity {

    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.layout_tab)
    TabLayout layoutTab;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    @Bind(R.id.title_right_img)
    ImageView titleRightImg;
    @Bind(R.id.title_right_img2)
    ImageView titleRightImg2;
    @Bind(R.id.layout_title)
    LinearLayout layoutTitle;

    private int curBlogType = 0;//当前页的帖子类型：0全部，1农技，2养生，3求助，4补贴，5其它, 6易货
    private PopupWindow mPopupWindow;
    private TabViewPagerAdapter pagerAdapter;
    private LaokeListFragment allFragment = new LaokeListFragment();
    private LaokeListFragment releaseedFragment = new LaokeListFragment();
    private LaokeListFragment joinedFragment = new LaokeListFragment();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_laoke);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        List<String> titles = new ArrayList<>();
        titles.add("全部");
        titles.add("发布的");
        titles.add("参与的");
        allFragment.setType("0");
        releaseedFragment.setType("1");
        joinedFragment.setType("2");
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(allFragment);
        fragments.add(releaseedFragment);
        fragments.add(joinedFragment);
        pagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewpager.setAdapter(pagerAdapter);
        viewpager.setOffscreenPageLimit(3);
        layoutTab.setTabMode(TabLayout.MODE_FIXED);
        layoutTab.setupWithViewPager(viewpager);
        layoutTab.setTabsFromPagerAdapter(pagerAdapter);
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
    }

    @Override
    protected void getExras() {

    }

    @Override
    protected void findView() {
    }

    @Override
    protected void setListener() {
        titleText.setText("助农");
        titleRightBtn.setVisibility(View.GONE);
        titleRightImg2.setImageResource(R.drawable.laoke_screen);
        titleRightImg.setVisibility(View.VISIBLE);
        titleRightImg2.setVisibility(View.VISIBLE);
    }

    /**
     * 显示筛选分类
     */
    private void showAssort() {
        initPopuptWindow();
        mPopupWindow.showAsDropDown(layoutTitle);
    }

    private void initPopuptWindow() {
        final View popView = mContext.getLayoutInflater().inflate(R.layout.pop_laoke_screening, null);
        TextView tvAll = (TextView) popView.findViewById(R.id.tv_all);
        TextView tvNonghuo = (TextView) popView.findViewById(R.id.tv_nonghuo);
        TextView tvYangsheng = (TextView) popView.findViewById(R.id.tv_yangsheng);
        TextView tvHelp = (TextView) popView.findViewById(R.id.tv_help);
        TextView tvButie = (TextView) popView.findViewById(R.id.tv_butie);
        TextView tvYiHuo = (TextView) popView.findViewById(R.id.tv_yihuo);
        TextView tvOther = (TextView) popView.findViewById(R.id.tv_other);
        switch (curBlogType) {//当前页的帖子类型：0全部，1农技，2养生，3求助，4补贴，5其它, 6易货
            case 0:
                tvAll.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            case 1:
                tvNonghuo.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            case 2:
                tvYangsheng.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            case 3:
                tvHelp.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            case 4:
                tvButie.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            case 5:
                tvOther.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            case 6:
                tvYiHuo.setTextColor(getResources().getColor(R.color.theme_orange));
                break;
            default:
                break;
        }
        tvAll.setOnClickListener(popListener);
        tvNonghuo.setOnClickListener(popListener);
        tvYangsheng.setOnClickListener(popListener);
        tvHelp.setOnClickListener(popListener);
        tvButie.setOnClickListener(popListener);
        tvOther.setOnClickListener(popListener);
        tvYiHuo.setOnClickListener(popListener);

        mPopupWindow = new PopupWindow(popView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        popView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int top = popView.findViewById(R.id.ll_laoke_screening).getTop();
                int bottom = popView.findViewById(R.id.ll_laoke_screening).getBottom();
                int y = (int) event.getY();
                int x = (int) event.getX();
                if (y > bottom || y < top) {
                    mPopupWindow.dismiss();
                }
                return true;
            }
        });
    }

    private OnClickListener popListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            mPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.tv_all://全部
                    allFragment.refreshData("0");
                    joinedFragment.refreshData("0");
                    releaseedFragment.refreshData("0");
                    curBlogType = 0;//当前页的帖子类型：0全部，1农技，2养生，3求助，4补贴，5其它, 6易货
                    break;
                case R.id.tv_help://求助
                    allFragment.refreshData("1");
                    joinedFragment.refreshData("1");
                    releaseedFragment.refreshData("1");
                    curBlogType = 3;
                    break;
                case R.id.tv_nonghuo://农技
                    allFragment.refreshData("2");
                    joinedFragment.refreshData("2");
                    releaseedFragment.refreshData("2");
                    curBlogType = 1;
                    break;
                case R.id.tv_yangsheng://养生
                    allFragment.refreshData("3");
                    joinedFragment.refreshData("3");
                    releaseedFragment.refreshData("3");
                    curBlogType = 2;
                    break;
                case R.id.tv_butie://补贴
                    allFragment.refreshData("4");
                    joinedFragment.refreshData("4");
                    releaseedFragment.refreshData("4");
                    curBlogType = 4;
                    break;
                case R.id.tv_other://其他
                    allFragment.refreshData("5");
                    joinedFragment.refreshData("5");
                    releaseedFragment.refreshData("5");
                    curBlogType = 5;
                    break;
                case R.id.tv_yihuo:
                    allFragment.refreshData("6");
                    joinedFragment.refreshData("6");
                    releaseedFragment.refreshData("6");
                    curBlogType = 6;
                    break;
                default:
                    break;
            }
        }
    };

    @OnClick({R.id.title_left_btn, R.id.title_right_img, R.id.title_right_img2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.title_right_img:
                Intent itSearch = new Intent(mContext, LaokeSearchActivity.class);
//                itSearch.putExtra("type", 6);
//                itSearch.putExtra("assort", "助农");
                startActivity(itSearch);
                break;
            case R.id.title_right_img2:
                showAssort();
                break;
        }
    }
}
